package com.kcnet.qk.controller.pyq;

import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.kcnet.qk.constant.Const;
import com.kcnet.qk.controller.BasePeopleController;
import com.kcnet.qk.util.NodeUtil;
import com.kcnet.qk.util.PreferenceHelper;
import com.kcnet.qk.util.RootShell;

import org.greenrobot.eventbus.EventBus;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.jesse.nativelogger.NLogger;

/**
 * Created by Administrator on 2018/7/9.
 * 朋友圈自动点赞
 */

public class PyqDzCTR extends BasePeopleController {

    private static String TAG = "朋友圈自动点赞";

    private static PyqDzCTR mRongCloudInstance;

    protected Map<String, Boolean> mRecordMap = Collections.synchronizedMap(new HashMap());

    /**
     * 获取RongCloud 实例。
     *
     * @return RongCloud。
     */
    public static PyqDzCTR getInstance() {
        if (mRongCloudInstance == null) {
            synchronized (PyqDzCTR.class) {

                if (mRongCloudInstance == null) {
                    mRongCloudInstance = new PyqDzCTR();
                }
            }
        }

        return mRongCloudInstance;
    }


    @Override
    public void doTask(AccessibilityEvent event) throws Exception {
        mType = event.getEventType();
        mClassName = (String) event.getClassName();

        if (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.WXUI.LauncherUI)) {
            NLogger.d(TAG,"1.进入微信首页 点击 发现 按钮");
            sleepTime(2000);
            tagTextClick("发现");

            sleepTime(1000);
            NLogger.d(TAG,"2.到达发现界面 点击 朋友圈");
            tagTextClick("朋友圈");
        }

        else if (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.WXUI.SnsTimeLineUI)) {
            NLogger.d(TAG, "3、到达朋友圈主页面 开始滚动列表");
            sleepTime(2000);
            RootShell.swipe(400,600,400,400);
        }
        else if (mType == AccessibilityEvent.TYPE_VIEW_SCROLLED && mClassName.equals(Const.LIST_VIEW)) {
          beginAddPeople();
        }

    }


    private void beginAddPeople() {




        AccessibilityNodeInfo scrllorNodeInfo;
        AccessibilityNodeInfo root = getRoot();
        if (root != null) {
            synchronized (mLock) {
                scrllorNodeInfo = NodeUtil.findNodeByClassNameAndTime(root, Const.LIST_VIEW, 0);
                for (int i = 0; i < scrllorNodeInfo.getChildCount(); i++) {

                    if(currentAddCount == maxAddCount){
                        currentAddCount=0;
                        stopTask();
                    }
                    AccessibilityNodeInfo nodeInfo = scrllorNodeInfo.getChild(i);
                    if(nodeInfo.getChildCount()<1){
                        continue;
                    }

                    List<AccessibilityNodeInfo> listPl = nodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/dg4");
                    if(!listPl.isEmpty()){
                        AccessibilityNodeInfo pl =  listPl.get(0);
                        NodeUtil.performClick(pl);

                        AccessibilityNodeInfo zan = findTextNodeInfoId("df6");

                            if(zan.getText().toString().equals("赞")){
                                NodeUtil.performClick(zan);
                                currentAddCount++;
                            }else {
                                NodeUtil.performClick(pl);
                            }
                      //  RootShell.swipe(400,600,400,400);
                        //sleepTime(500);
                    }

                    if (i == scrllorNodeInfo.getChildCount() - 1) {
                          i = 0;
                            RootShell.swipe(400,900,400,600);
                            sleepTime(500);
                            break;
                    }

                }

            }
        }
        }

    /**
     *
     * @param maxAddCount 点赞数量
     * @param minAddTime
     * @param maxAddTime
     */
    public void setParms(int maxAddCount,int minAddTime,int maxAddTime){
        //  stopTask();
        EventBus.getDefault().post(new Boolean(true));
        PreferenceHelper.putBoolean(Const.PREF_KEY_STOP_AUTO_FLAG, true);
        this.maxAddCount = maxAddCount;
        this.minAddTime = minAddTime;
        this.maxAddTime = maxAddTime;
        this.currentAddCount = 0;
        mRecordMap.clear();

    }

}
